package com.qfedu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.RouteEntity;
import com.qfedu.entity.RouteGroupEntity;
import com.qfedu.service.RouteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller

public class RouteController {

    @Reference(check = false)
    RouteService routeService;


    @RequestMapping("/queryList")
    public String queryList(HttpServletRequest request, RouteEntity route) {


        RouteEntity re = trim(route);

        PageInfo<RouteEntity> pageInfo = routeService.queryList(re);

        List<RouteGroupEntity> grouplist = routeService.queryGroupList();

        request.getSession().setAttribute("pageInfo", pageInfo);
        request.getSession().setAttribute("groupList", grouplist);

        request.getSession().setAttribute("re",re);

        return "route";
    }

    public RouteEntity trim(RouteEntity re){

        if (re.getSplace() != null){
            String sp = re.getSplace().trim();
            re.setSplace(sp);
        }
        if (re.getAplace() != null){
            String ap = re.getAplace().trim();
            re.setAplace(ap);
        }
        if (re.getTimes() != null){
            String ts = re.getTimes().trim();
            re.setTimes(ts);
        }
        return re;

    }

    @RequestMapping("/queryGroup")
    public String queryGroup(int rgid, HttpServletRequest request) {

        RouteEntity routeEntity = new RouteEntity();
        routeEntity.setRgid(rgid);

        PageInfo<RouteEntity> pageInfo = routeService.queryList(routeEntity);

        List<RouteGroupEntity> grouplist = routeService.queryGroupList();

        request.getSession().setAttribute("pageInfo", pageInfo);
        request.getSession().setAttribute("groupList", grouplist);

        return "route";
    }

    @RequestMapping("/queryByAddress")
    public String queryByAddress(String splace, String aplace,HttpServletRequest request) {
        RouteEntity routeEntity = new RouteEntity();
        if (splace == null && aplace != null){
            routeEntity.setAplace(aplace);
        }

        if (aplace == null && splace != null){
            routeEntity.setSplace(splace);
        }

        PageInfo<RouteEntity> pageInfo = routeService.queryList(routeEntity);

        List<RouteGroupEntity> grouplist = routeService.queryGroupList();

        request.getSession().setAttribute("pageInfo", pageInfo);
        request.getSession().setAttribute("groupList", grouplist);


        return "route";
    }


    @RequestMapping("jumpQBPlace")
    public String jumpQBPlace(HttpServletRequest request) {

        return "routetime";
    }

    @RequestMapping("/queryByPlace")

    public String queryByPlace(RouteEntity re,HttpServletRequest request) {

        PageInfo<RouteEntity> pageInfo = routeService.queryList(re);

        request.getSession().setAttribute("pageInfo1", pageInfo);
        request.getSession().setAttribute("re1",re);

        return "routetime";
    }


    @RequestMapping("jumpTime")
    public String jumpTime(HttpServletRequest request) {

        return "routetimes";
    }
    @RequestMapping("jumpRgName")
    public String jumpName(HttpServletRequest request) {

        List<RouteGroupEntity> list = routeService.queryGroupList();
        request.getSession().setAttribute("gr",list);

        return "routename";
    }

    @RequestMapping("/queryByRgName")
    public String queryByRgName(RouteEntity re,HttpServletRequest request) {

        PageInfo<RouteEntity> pageInfo = routeService.queryList(re);

        request.getSession().setAttribute("pageInfo2", pageInfo);
        request.getSession().setAttribute("re2",re);

        return "routename";
    }


    @RequestMapping("/queryTime")
    public String queryTime(RouteEntity re,HttpServletRequest request) {

        PageInfo<RouteEntity> pageInfo = routeService.queryList(re);

        request.getSession().setAttribute("pageInfo3", pageInfo);
        request.getSession().setAttribute("re3",re);

        return "routetimes";
    }
}
